What I would recommend is reading your binary value as characters (string). Then you can enter the entire binary value as discrete digits.
Then loop through that array until you hit the null at the end of the list.
Testing for '0' and '1' will allow you to accumulate each digit and 'add' them to the sum.